package se.lightsources.manual;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

/**
 * 
 * Copyright (c) 2008 Joakim Back
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * @author Joakim Back
 *
 */
public abstract class AbstractFrame {
	class PaintArea extends Component implements MouseListener {
		private static final long serialVersionUID = -1307145744027664250L;

		private AbstractFrame parent;

		public PaintArea(AbstractFrame parent, int width, int height) {
			this.parent = parent;
			this.setPreferredSize(new Dimension(width, height));
			this.addMouseListener(this);
		}

		public void paint(Graphics g) {
			parent.draw(g);
		}

		public void mouseClicked(MouseEvent e) {
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}

		public void mousePressed(MouseEvent e) {
			parent.mousePressed(e.getX(), e.getY());
		}

		public void mouseReleased(MouseEvent e) {
		}
	}

	protected int width;

	protected int height;

	private JFrame frame;

	public AbstractFrame(String title, int width, int height) {
		this.width = width;

		this.height = height;

		init();

		frame = new JFrame(title);
		PaintArea area = new PaintArea(this, width, height);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(area);
		frame.pack();
		frame.setVisible(true);
	}

	protected void repaint() {
		frame.repaint();
	}

	protected abstract void init();

	protected abstract void draw(Graphics g);

	protected abstract void mousePressed(int x, int y);
}
